﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConfigInfoWebPlatform.API.BusinessCommon
{
    /// <summary>
    /// Base64加解密
    /// </summary>
    public static class Base64Helper
    {
        // Base64 编码  
        /// <summary>  
        /// 对字符串进行Base64编码  
        /// </summary>  
        /// <param name="codepage">代码页,utf-8:65001，gb2132:936</param>  
        /// <param name="decode">需要加密的字符串</param>  
        /// <returns></returns>  
        public static string EnCodeBase64(int codepage, string decode)
        {
            string encode = "";

            try
            {
                byte[] bytes = Encoding.GetEncoding(codepage).GetBytes(decode);
                encode = Convert.ToBase64String(bytes);
            }
            catch
            {
                encode = "";
            }

            return encode;
        }

        // Base64 解码  
        /// <summary>  
        /// 对Base64编码的字符串进行解码  
        /// </summary>  
        /// <param name="codepage">代码页,utf-8:65001，gb2132:936</param>  
        /// <param name="encode">需要解码的字符串</param>  
        /// <returns></returns>  
        public static string DeCodeBase64(int codepage, string encode)
        {
            string decode = "";
            try
            {
                byte[] bytes = Convert.FromBase64String(encode);
                decode = Encoding.GetEncoding(codepage).GetString(bytes);
            }
            catch
            {
                decode = encode;
            }

            return decode;
        }
    }
}
